package pers.cnzdy.ipcserver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class QuizService extends Service {
    private final String TAG = "Server";
    private List<Quiz> QuizList;

    private final IQuiz.Stub stub = new IQuiz.Stub() {
        @Override
        public List<Quiz> getQuizList() throws RemoteException {
            return QuizList;
        }

        @Override
        public void addQuiz(Quiz quiz) throws RemoteException {
            if (quiz != null) {
                Log.e(TAG, "服务器新增一道题目");
                QuizList.add(quiz);
            } else {
                Log.e(TAG, "接收到一个空对象");
            }
        }

    };

    public QuizService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        QuizList = new ArrayList<>();
        initData();
    }

    private void initData() {
        Quiz Quiz1 = new Quiz("简述Intent过滤器的定义和功能。");
        Quiz Quiz2 = new Quiz("说明Handler异步消息处理的流程。");
        Quiz Quiz3 = new Quiz("简述Service的基本原理和用途。");
        Quiz Quiz4 = new Quiz("简述移动计算的主要特点。");
        Quiz Quiz5 = new Quiz("什么是多路复用？解释频分、时分和码分多路复用。");
        Quiz Quiz6 = new Quiz("简述竞争信道的工作过程。");

        QuizList.add(Quiz1);
        QuizList.add(Quiz2);
        QuizList.add(Quiz3);
        QuizList.add(Quiz4);
        QuizList.add(Quiz5);
        QuizList.add(Quiz6);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return stub;
    }

}